﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TestCaseModel;

namespace TestCaseGenerator.Controls
{
    public partial class GeneratorOptionsUi : UserControl
    {
        private bool eventAdded = false;

        /// <summary>
        /// Is fired when generating of the cases is being
        /// requested by the user (e.g. by clicking the button)
        /// </summary>
        public event EventHandler GeneratingRequested;

        /// <summary>
        /// This event is triggered whenever a value
        /// of the CurrentOptions property changes
        /// </summary>
        public event EventHandler OptionsChanged;

        /// <summary>
        /// This event will be triggered in case the
        /// button check model has been clicked
        /// </summary>
        public event EventHandler CheckRequested;

        GeneratorOptions currentOptions = null;
        /// <summary>
        /// The reference to the GeneratorOptions
        /// instance that shall be bound to the
        /// user interface.
        /// </summary>
        public GeneratorOptions CurrentOptions
        {
            get { return currentOptions; }
            set 
            { 
                currentOptions = value;
                if (currentOptions != null)
                {
                    if (!eventAdded)
                    {
                        currentOptions.PropertyChanged += this.Resync;
                        eventAdded = true;
                    }
                    Resync();
                }
            }
        }

        public GeneratorOptionsUi()
        {
            InitializeComponent();
        }

        private void GeneratorOptionsUi_Load(object sender, EventArgs e)
        {
            this.Enabled = false;
        }

        /// <summary>
        /// Resync the UI with the underlying data
        /// </summary>
        public void Resync()
        {
            this.Enabled = true;

            cboGenerator.SelectedIndex = (int) currentOptions.GeneratorType;
            numDepth.Value = currentOptions.CombinationDepth;
            chkApplyRules.Checked = currentOptions.ApplyRules;
            chkNullConds.Checked = currentOptions.ApplyNullConditions;
            chkExampleData.Checked = currentOptions.GenerateWithExampleData;
            chkMergeResults.Checked = currentOptions.TryToMergeWithEditedResults;
        }

        private void OptsChanged()
        {
            if (OptionsChanged != null)
                OptionsChanged(this, null);
        }


        private void cboGenerator_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentOptions.GeneratorType = (GlobalSettings.TcGenerator) cboGenerator.SelectedIndex;
            OptsChanged();
        }

        
        private void numDepth_ValueChanged(object sender, EventArgs e)
        {
            currentOptions.CombinationDepth = (int) numDepth.Value;
            OptsChanged();
        }

        private void chkNullConds_CheckedChanged(object sender, EventArgs e)
        {
            currentOptions.ApplyNullConditions = chkNullConds.Checked;
            OptsChanged();
        }

        private void chkExampleData_CheckedChanged(object sender, EventArgs e)
        {
            currentOptions.GenerateWithExampleData = chkExampleData.Checked;
            OptsChanged();
        }

        private void chkMergeResults_CheckedChanged(object sender, EventArgs e)
        {
            currentOptions.TryToMergeWithEditedResults = chkExampleData.Checked;
            OptsChanged();
        }

        private void butGenerate_Click(object sender, EventArgs e)
        {
            if (GeneratingRequested != null)
                GeneratingRequested(this, null);
        }

        private void chkApplyRules_CheckedChanged(object sender, EventArgs e)
        {
            currentOptions.ApplyRules = chkApplyRules.Checked;
            OptsChanged();
        }

        private void butCheckModel_Click(object sender, EventArgs e)
        {
            if (CheckRequested != null)
                CheckRequested(this, null);
        }
    }
}
